package com.demo.oss.controller;


import com.demo.oss.service.FileService;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@RestController
@RequestMapping("/oss")
public class OssController {


    @Resource
    private FileService fileService;

    @PostMapping("/upload")
    public String upload(

            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "module",required = false,defaultValue = "test") String module){
        try {
            InputStream inputStream = file.getInputStream();
            String originalFilename = file.getOriginalFilename();
            String url = fileService.upload(inputStream, module, originalFilename);

            return url;
        } catch (IOException e) {
           e.getMessage();
           return "999";
        }
    }

    @PostMapping("/batchupload")
    public String batchupload(
            @RequestParam("filedatas") List<MultipartFile> files,
            @RequestParam(value = "module",required = false,defaultValue = "sgu-edu") String module){
        String url="";
        try {

            for(MultipartFile file:files){
                MultipartFile temp=file;
                InputStream inputStream = temp.getInputStream();
                String originalFilename = temp.getOriginalFilename();
                 url = fileService.upload(inputStream, module, originalFilename);


            }

        } catch (IOException e) {
            e.getMessage();
            return "999";
        }
        return url;
    }
    @GetMapping("/dele")
    public void delete(){
        fileService.removeFile("mogu-blog/2022/09/13/1663077482178.jpeg");
    }



}
